package com.ce.server.pojo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ce.server.common.CustomAuthorityDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-14
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_admin")
@ApiModel(value = "Admin对象", description = "")
// @JsonInclude(JsonInclude.Include.NON_NULL)
public class Admin implements Serializable, UserDetails {


  private static final long serialVersionUID = 1L;

  @ApiModelProperty(value = "用户id")
  @TableId(value = "id", type = IdType.AUTO)
  private Integer id;

  @ApiModelProperty(value = "用户账号")
  @Excel(name = "工号")
  private String wno;

  @ApiModelProperty(value = "用户名")
  @Excel(name = "姓名")
  private String wname;

  @ApiModelProperty(value = "加密后")
  private String password;

  @ApiModelProperty(value = "0代表男，1代表女")
  @Excel(name = "性别")
  private String gender;

  @ApiModelProperty(value = "年龄")
  @Excel(name = "年龄")
  private Integer age;

  @ApiModelProperty(value = "联系地址")
  @Excel(name = "联系地址")
  private String address;

  @ApiModelProperty(value = "手机号码")
  @Excel(name = "手机号码")
  private String telephone;

  @ApiModelProperty(value = "0为未启用，1为已启用")
  @Getter(value = AccessLevel.NONE)
  private String enabled;

  @ApiModelProperty(value = "出生日期")
  @Excel(name = "出生日期")
  private LocalDate birthday;

  @ApiModelProperty(value = "身份证号")
  @Excel(name = "身份证号码")
  private String isCard;

  @ApiModelProperty(value = "头像地址")
  private String userface;

  @TableField(exist = false)
  private String doorway;

  @ApiModelProperty(value = "民族id")
  private Integer nationId;

  @ApiModelProperty(value = "籍贯")
  @Excel(name = "籍贯")
  private String nativePlace;

  @ApiModelProperty(value = "民族")
  @TableField(exist = false)
  @ExcelEntity
  private Nation nation;

  @ApiModelProperty(value = "职位ID")
  private Integer posId;

  @ApiModelProperty(value = "职位")
  @TableField(exist = false)
  @ExcelEntity
  private Position position;

  @ApiModelProperty(value = "政治面貌id")
  private Integer politicId;

  @ApiModelProperty(value = "政治面貌")
  @TableField(exist = false)
  @ExcelEntity
  private PoliticsStatus politicsStatus;


  @ApiModelProperty(value = "角色列表")
  @TableField(exist = false)
  private List<Role> roles;


  @Override
  @JsonDeserialize(using = CustomAuthorityDeserializer.class)
  public Collection<? extends GrantedAuthority> getAuthorities() {
    List<SimpleGrantedAuthority> authorities = roles
            .stream()
            .map(role -> new SimpleGrantedAuthority(role.getName()))
            .collect(Collectors.toList());
    return authorities;
  }

  @Override
  public String getUsername() {
    return wno;
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return true;
  }
}
